package net.lovesrc.support.service.generator.idcard;

import java.util.HashSet;
import java.util.Random;
import java.util.Set;

/**
 * 身份证生成手动实现
 * @author gelic
 * Created in 10:10 2018/10/30
 * @since
 */
public class IDCardGeneratorServiceImpl implements IDCardGeneratorService {

    @Override
    public IDCard generate() {

        //no 六位数字地址码，八位数字出生日期码，三位数字顺序码和一位数字校验码。
        Random random = new Random();

        boolean male = IDCardGeneratorUtil.getGender(random);

        String orderCode = IDCardGeneratorUtil.generateOrderCode(random, male);
        String birthday = IDCardGeneratorUtil.generateBirthday(random);
        String name = IDCardGeneratorUtil.getName(random, male);
        String districtId = IDCardGeneratorUtil.getDistrictId(random);
        StringBuilder sb = new StringBuilder().append(districtId).append(birthday).append(orderCode);
        String checkNum = IDCardGeneratorUtil.getIDCardCheckNum(sb.toString());
        sb.append(checkNum);

        String gender = male ? "男" : "女";
        return new IDCard(name, sb.toString(), gender, IDCardGeneratorUtil.getAddr(districtId));
    }

    @Override
    public Set<IDCard> generate(int size) {
        Set<IDCard> idCards = new HashSet<>();
        for (; idCards.size() < size; ) {
            idCards.add(generate());
        }
        return idCards;
    }

    public static void main(String[] args) {
        IDCard generate = new IDCardGeneratorServiceImpl().generate();
        System.out.println(generate);
    }
}
